/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.config.OnMyConfigHandleAdapter;
import com.zhuangfei.ohos_timetableview.dialog.DialogUtil;
import com.zhuangfei.ohos_timetableview.dialog.ToastViewDialog;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.model.ScheduleConfig;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

import java.util.List;
import java.util.Set;

/**
 * LocalConfigSlice
 *
 * @since 2021-03-29
 */
public class LocalConfigSlice extends BaseAbilitySlice {
    /**
     * CONFIG_NAME
     */
    public static final String CONFIG_NAME = "local_config_name";
    /**
     * SPIN_MUNE
     */
    private static final String[] SPIN_MUNE = {"加载本地配置", "清除本地配置", "导出配置至文本", "加载配置文本"};

    TimetableView timetableView;
    MaterialSpinner moreButton;
    Text titleTextView;
    Intent intents;
    List<MySubject> mySubjects;
    ScheduleConfig mScheduleConfig;
    Set<String> configSet;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_local_config);
        initViews();
        intents = intent;
    }

    private void initViews() {
        timetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        moreButton = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        titleTextView = (Text) findComponentById(ResourceTable.Id_id_title);

        mySubjects = SubjectRepertory.loadDefaultSubjects2();
        mySubjects.addAll(SubjectRepertory.loadDefaultSubjects());

        moreButton.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                selectLayout(position);
                view.setShowFalse();

            }
        });
        moreButton.setItems(SPIN_MUNE);
        moreButton.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

            @Override
            public void onNothingSelected(MaterialSpinner spinner) {
                spinner.setShowFalse();
            }
        });

        initTimetableView();
    }


    private void initTimetableView() {
        mScheduleConfig = new ScheduleConfig(this);
        mScheduleConfig.setConfigName(CONFIG_NAME);

        /**
         * 获取控件
         */
        timetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .maxSlideItem(10)
                .configName(CONFIG_NAME)
                .callback(new OnMyConfigHandleAdapter())
                .showView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                loadLocalConfig();
                break;
            case 1:
                clearLocalConfig();
                break;
            case 2:
                exportLocalConfig();
                break;
            case 3:
                loadLocalConfigSet();
                break;
            default:
                break;
        }
    }

    /**
     * 设置本地配置项并使其生效
     */
    protected void loadLocalConfig() {
        mScheduleConfig.put(OnMyConfigHandleAdapter.CONFIG_SHOW_WEEKENDS, OnMyConfigHandleAdapter.VALUE_FALSE)
                .put(OnMyConfigHandleAdapter.CONFIG_SHOW_NOT_CUR_WEEK, OnMyConfigHandleAdapter.VALUE_TRUE)
                .put(OnMyConfigHandleAdapter.CONFIG_USERLESSS_COLOR, "#000000")
                .commit();
        timetableView.updateView();
    }

    /**
     * 清除本地配置项
     */
    protected void clearLocalConfig() {
        mScheduleConfig.clear();
        ToastViewDialog.toast(this, "清除成功，下次进入生效!");
    }

    /**
     * 本地配置导出到文本
     */
    private void exportLocalConfig() {
        Set<String> set = mScheduleConfig.export();
        configSet = set;
        StringBuffer content = new StringBuffer();
        for (String s : set) {
            content.append(s + "\n");
        }
        String str = content.toString();
        DialogUtil.show(this, "配置导出", str);


    }

    /**
     * 从文本导入本地配置
     */
    private void loadLocalConfigSet() {
        if (configSet == null) {
            DialogUtil.show(this, "配置导入", "还没有导出，先导出再来试试吧");
        } else {
            mScheduleConfig.load(configSet);
            ToastViewDialog.toast(this, "配置已生效");
        }
        timetableView.updateView();
    }
}
